package com.ihr360.insurance.controller.ledger;

import com.alibaba.fastjson.JSONObject;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.excel.entity.ExportParams;
import com.ihr360.excel.vo.ImportRequest;
import com.ihr360.insurance.entity.request.MonthlyLedgerRequest;
import com.ihr360.insurance.service.MonthlyLedgerService;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @author Stone.Shi
 * @description 社保公积金台账导入、导出
 * @date 2018-05-16 10:02:34.
 */
@RestController
@RequestMapping("/monthlyLedgers")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.STANDING_BOOK_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.STANDING_BOOK_NAME)
@DisableResponseAdvice
public class MonthlyLedgerImportController {

    @Autowired
    private MonthlyLedgerService monthlyLedgerService;

    @FunctionDefinition(functionName = "获取导出的台账数据", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/getExportData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ExportParams<List<Object>> getExportData(@RequestParam String companyId,
                                                    @RequestParam String userId,
                                                    @RequestParam String requestString,
                                                    @RequestBody Map<Long, String> departmentFullNames) throws IOException {
        MonthlyLedgerRequest req = new MonthlyLedgerRequest();
        try {
            req = JSONObject.parseObject(requestString, MonthlyLedgerRequest.class);
        } catch (Exception e) {
            ValidationUtils.reject("", "下载参数错误");
        }
        return monthlyLedgerService.getExportData(companyId, userId, req, departmentFullNames);
    }

    @FunctionDefinition(functionName = "下载台账数据导入模板", functionCode = ModuleCodeStore.Function.IMPORT)
    @RequestMapping(value = "/template", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void downloadTemplate(@RequestParam String companyId,
                                 @RequestParam Integer ledgerYear,
                                 @RequestParam Integer ledgerMonth,
                                 HttpServletResponse response) throws IOException {
        response.setHeader("x-filename", URLEncoder.encode("社保公积金台账模板.xls", "UTF-8"));
        monthlyLedgerService.downloadTemplate(companyId, ledgerYear, ledgerMonth, response.getOutputStream());
    }

    @FunctionDefinition(functionName = "导入台账数据", functionCode = ModuleCodeStore.Function.IMPORT)
    @PostMapping(value = "/import", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultInfo importLedger(@RequestParam String companyId,
                                   @RequestParam String userId,
                                   @RequestParam Integer ledgerYear,
                                   @RequestParam Integer ledgerMonth,
                                   @RequestBody ImportRequest importRequest) {
        monthlyLedgerService.importExcel(companyId, userId, ledgerYear, ledgerMonth, importRequest);
        return new ResultInfo.Builder<>().success();
    }
}
